# -*- coding: utf-8 -*-
import os

from MainEngines  import *
from aqua.qsshelper import QSSHelper
from forms.marketform import *
from forms.vgform import *
from forms.workspace import *
from forms.webvgform import *

class MainWindow(QtGui.QMainWindow):
    def __init__(self,eventEngine, mainEngine):
        super(MainWindow, self).__init__()
        self.setWindowTitle(u'Level2行情实时走势')
        qss = QSSHelper.open_qss(os.path.join('aqua', 'aqua.qss'))
        self.setStyleSheet(qss)
        self.__eventEngine = eventEngine
        self.__mainEngine = mainEngine
        self._addedDockWidgets = []
        self.workspace = DesktopWorkspace(self)
        self.setCentralWidget(self.workspace)
        self.createActions()
        self.createMenus()
        self.createToolBars()
        self.createStatusBar()
        self.createDockWindows()
        self.setCorner(Qt.TopLeftCorner, Qt.LeftDockWidgetArea)
        self.setCorner(Qt.TopRightCorner, Qt.RightDockWidgetArea)
        self.setCorner(Qt.BottomLeftCorner, Qt.BottomDockWidgetArea)
        self.setCorner(Qt.BottomRightCorner, Qt.BottomDockWidgetArea)



    def paintEvent(self, event):
        pass

        #print  self.width()
        #print  self.height()

    def createMenus(self):
        self.loginMenu = self.menuBar().addMenu(u"&文件")
        self.loginMenu.addAction(self.loginAct)
        self.loginMenu.addSeparator()
        self.helpMenu = self.menuBar().addMenu(u"&窗口")
        self.helpMenu.addAction(self.marketAct)


    def createToolBars(self):
        self.topToolBar = QtGui.QToolBar()
        self.topToolBar.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)

        self.addToolBar(self.topToolBar)
        self.topToolBar.addAction(self.buyAct)

    def createStatusBar(self):
        self.statusBar().showMessage("Ready")

    def createActions(self):
        self.loginAct = QtGui.QAction(QtGui.QIcon('images/login.png'),
                                          u'登录', self, shortcut=QtGui.QKeySequence.New,
                                          statusTip=u"登录交易系统",
                                          triggered=self._onLogin)

        self.buyAct = QtGui.QAction(QtGui.QIcon('images/buy.png'),
                                      u'委托买入', self, shortcut=QtGui.QKeySequence.New,
                                      statusTip=u"委托买入",
                                      triggered=self._onLogin)

        self.marketAct = QtGui.QAction(QtGui.QIcon('images/buy.png'),
                              u'行情窗口', self, shortcut=QtGui.QKeySequence.New,
                              statusTip=u"行情窗口",
                              triggered=self.createMdiChild)



    def _onLogin(self):
        self.marketWinow = MarketDataMonitorForm(self.__eventEngine, self)
        self.addDockWidget(Qt.LeftDockWidgetArea, self.marketWinow)

    def createMdiChild(self):

        child = WebVGForm(self.__eventEngine, self)
        self.workspace.addSubWindow(child)



        return child

    def createDockWindows(self):

        self.marketWindow  = MarketDataMonitorForm(self.__eventEngine,u"市场行情", self)
        self.addDockWidget(Qt.LeftDockWidgetArea, self.marketWindow)





        # 停靠窗口3
        dock3 = DockWidget(self,u"交易信息")


        dock3.setFeatures( QtGui.QDockWidget.DockWidgetClosable | QtGui.QDockWidget.DockWidgetVerticalTitleBar)
        te3 =QtGui.QTextEdit(u"窗口3,可在Main Window任意位置停靠，可浮动，可关闭")
        dock3.addWidget(te3)
        self.addDockWidget(QtCore.Qt.BottomDockWidgetArea, dock3)

    def sizeHint(self):
        return QSize(900, 560)

    def addDockWidget(self, area, dock):
        assert not dock in self._addedDockWidgets
        self._addedDockWidgets.append(dock)
        if self.restoreDockWidget(dock):
            return
        else:
            QtGui.QMainWindow.addDockWidget(self, area, dock)

    def removeDockWidget(self, dock):
        pass  # not a plugin API method
        """Remove dock widget"""
        assert dock in self._addedDockWidgets
        self._addedDockWidgets.remove(dock)
        QtGui.QMainWindow.removeDockWidget(self, dock)

    def closeEvent(self, event):
        """退出事件处理"""
        reply = QtGui.QMessageBox.question(self, u'退出',
                                           u'确认退出?', QtGui.QMessageBox.Yes |
                                           QtGui.QMessageBox.No, QtGui.QMessageBox.No)

        if reply == QtGui.QMessageBox.Yes:
            self.__mainEngine.exit()
            event.accept()
        else:
            event.ignore()


